﻿(function (window) {
    //视频数组
    resources.videoArray = [];
    //音频数组
    resources.audioArray = [];

    //创建音频对象
    var audio = {};

    //创建视频对象
    var video = {};

    /*
        加载视频
        参数:
          --名称
          --文件名称
    */
    video.load = function (name, fileName, fun) {
        var v = page.creatObject(name, "video");
        v.style.display = "none";
        v.addEventListener("loadeddata", function () {
            resources.videoArray[resources.videoArray.length] = { name: name, video: v };
            resources.loadDoneNum++;
            if (fun) fun(this);
            resources.checkLoad();
        }, false);
        v.onerror = function () {
            throw "加载视频对象\"" + name + "\"失败";
        };
        v.src = fileName;
        v.setAttribute("preload", "preload");
        resources.fileNum++;
        resources.checkLoad();
    };

    /*
        获取视频
        参数:
          --名称
    */
    video.get = function (name) {
        for (var i = resources.videoArray.length; i--;)
        {
            if (name === resources.videoArray[i].name)
            {
                return resources.videoArray[i].video;
            }
        }
    };

    /*
        移除视频
        参数:
          --名称
    */
    video.remove = function (name) {
        for (var i = resources.videoArray.length; i--;)
        {
            if (name === resources.videoArray[i].name)
            {
                resources.videoArray[i] == null;
                document.body.removeChild(document.getElementById(name));
                delete resources.videoArray[i];
            }
        }
    };

    /*
        清空视频
    */
    video.clear = function () {
        for (var i = resources.videoArray.length; i--;)
        {
            resources.videoArray[i] == null;
            document.body.removeChild(document.getElementById(name));
            delete resources.videoArray[i];
        }
    };

    /*
        加载音频
        参数:
          --名称
          --文件名称
          --额外设置
    */
    audio.load = function (name, fileName, fun) {
        var o = page.creatObject(name, "audio");
        o.addEventListener("loadeddata", function () {
            resources.loadDoneNum++;
            resources.audioArray.add({ name: name, audio: this });
            resources.checkLoad();
        }, false);
        o.onerror = function () {
            if (this.error.code == 4)
            {
                throw "无法播放音频";
                resources.loadDoneNum++;
                checkLoad();
            }
            else
            {
                throw "创建音频对象\"" + name + "\"失败";
                return;
            }
        }
        o.setAttribute("preload", "preload");
        if (fun)
        {
            fun(o);
        }
        if (fileName.indexOf('!') !== -1)
        {
            o.src = fileName.replace('!', '');
        }
        else
        {
            o.src = fileName + checker.supportAudioFormat();
        }
        resources.fileNum++;
        resources.checkLoad();
    };

    /*
        获取音频
        参数:
          --名称
    */
    audio.get = function (name) {
        var aa = resources.audioArray;
        for (var i = aa.length; i--;)
        {
            if (name === aa[i].name)
            {
                return aa[i].audio;
            }
        }
    };

    /*
        移除音频
        参数:
          --名称
    */
    audio.remove = function (name) {
        var aa = resources.audioArray;
        for (var i = aa.length; i--;)
        {
            if (name === aa[i].name)
            {
                document.body.removeChild(document.getElementById(name));
                resources.audioArray = aa.remove(i);
            }
        }
    };

    /*
        清空音频
    */
    audio.clear = function () {
        for (var i = resources.audioArray.length; i--;)
        {
            audio.remove(resources.audioArray[i]);
        }
    };

    /*
        检查是否正在加载
    */
    resources.checkLoad = function () {
        if (resources.fileNum === resources.loadDoneNum) resources.isLoading = false;
        else resources.isLoading = true;
    }

    //将对象公开
    window.audio = audio;
    window.video = video;
})(window);